<template>
	<view class="apply_content">
		<view class="apply_list">
			<view class="apply_item">
				<view class="apply_item_name">公司名称：</view>
				<input type="text" placeholder="请输入公司名称" maxlength="30" v-model="userInfo.companyName" />
			</view>
			<view class="apply_item">
				<view class="apply_item_name">负责人姓名：</view>
				<input type="text" placeholder="请输入负责人姓名" maxlength="30" v-model="userInfo.principalName" />
			</view>
			<view class="apply_item">
				<view class="apply_item_name">负责人手机号：</view>
				<view class="">{{userInfo.phone}}</view>
			</view>
			<view class="apply_item">
				<view class="apply_item_name">联系地址：</view>
				<input type="text" placeholder="请输入联系地址" maxlength="30" v-model="userInfo.address" />
			</view>
			<view class="apply_item">
				<view class="apply_item_name">业务经理</view>
				<picker :range="manager_list" @change="manager_list_event" :value="manager_value" range-key="userName"
					class="picker">
					<view class="">
						{{((manager_list[manager_value] || null) == null) ? '请选择' : manager_list[manager_value]['userName']}}
					</view>
				</picker>
			</view>
		</view>
		<view class="apply_btn">
			<button @click="submit_envent">提交</button>
		</view>
		<view class="footer">
			<view class="footer_item" @tap="telFun" :data-tel="dtel">
				<view class="footer_item_name">实体店加盟热线：邓总</view>
				<view class="footer_item_phone">
					<image src="https://t1.chowtaiking.com.cn/img/ruoyi/2025/03/29/43705e81129544c0984dddc3979c28f4.png" mode="widthFix"></image>
					<view class="">{{dtel}}</view>
				</view>
			</view>
			<view class="footer_item" @tap="telFun" :data-tel="wtel">
				<view class="footer_item_name">电商店加盟热线：翁总</view>
				<view class="footer_item_phone">
					<image src="https://t1.chowtaiking.com.cn/img/ruoyi/2025/03/29/0d448e51e1374fa1b5f12f83d5c79737.png" mode="widthFix"></image>
					<view class="">{{wtel}}</view>
				</view>
			</view>
			<view class="footer_item">
				<view class="footer_item_name">地址:</view>
				<view class="footer_item_phone">深圳市罗湖区水贝工业区19栋7楼周大金珠宝</view>
			</view>
		</view>
	</view>
</template>

<script>
	const app = getApp();
	export default {
		data() {
			return {
				params: null,
				entryTime: 0,
				userInfo: {
					companyName: '',
					principalName: '',
					phone: '',
					address: ''
				},
				manager_value: "",
				salesmanId: '',
				manager_list: [],
				dtel: '13144444455',
				wtel: '18144822222',
			}
		},
		onShareAppMessage(res) {
			return {
				title: app.globalData.data.tenantName,
				path: "pages/apply/apply?numberId=" + app.globalData.data.numberId,
				mpId: app.globalData.data.appid
			}
		},
		onLoad(params) {
			if (typeof(params) != 'undefined') {
				var numberId = params.numberId || '';
				if (numberId != '') {
					uni.clearStorageSync();
					app.globalData.data.numberId = numberId;
				}
			}
		},
		onShow() {
			// 记录用户进来时间
			this.entryTime = Date.now();
			this.init();
		},
		onHide() {
			this.OnEntryTime();
		},
		methods: {
			init(e) {
				var user = app.globalData.get_user_info(this, "init", "");
				if (user != false) {
					// 用户未绑定用户则转到登录页面
					if (app.globalData.user_is_need_login(user)) {
						uni.navigateTo({
							url: "/pages/login/login?event_callback=init"
						})

					} else {
						this.get_data();
						this.managerquery();
					}
				}
			},
			// 选业务经理
			manager_list_event(e) {
				var index = e.detail.value || 0;
				if (index >= 0) {
					var data = this.manager_list[index]['userName'];
					this.manager_value = index;
					this.salesmanId = this.manager_list[index]['userId'];
				}
			},
			get_data() {
				var data = {}
				uni.request({
					url: app.globalData.get_request_url("personal/index"),
					data: data,
					method: "POST",
					success: (res) => {
						if (res.data.code == 200) {
							let data = res.data.data;
							this.userInfo.phone = data.phone;
						}
					}
				})
			},
			// 获取业务经理
			managerquery() {
				uni.request({
					url: app.globalData.get_request_url("salesman"),
					method: 'POST',
					data: {},
					dataType: 'json',
					success: res => {
						if (res.data.code == 200) {
							var data = res.data.data;
							this.manager_list = data;
						} else {
							uni.showToast({
								title: res.data.msg,
								icon: 'none',
								duration: 2000
							})
						}
					},
					fail: () => {
						app.globalData.showToast('服务器请求出错');
					}
				});
			},
			submit_envent() {
				var post_data = {
					companyName: this.userInfo.companyName,
					principalName: this.userInfo.principalName,
					phone: this.userInfo.phone,
					address: this.userInfo.address,
					salesmanId: this.salesmanId,
				}
				if (!post_data.companyName) {
					uni.showToast({
						title: '请填写公司名称',
						icon: 'none',
						duration: 2000
					})
					return;
				}
				if (!post_data.principalName) {
					uni.showToast({
						title: '请填写负责人姓名',
						icon: 'none',
						duration: 2000
					})
					return;
				}
				if (!post_data.address) {
					uni.showToast({
						title: '请填写联系地址',
						icon: 'none',
						duration: 2000
					})
					return;
				}

				if (!post_data.salesmanId) {
					uni.showToast({
						title: '请选择业务经理',
						icon: 'none',
						duration: 2000
					})
					return;
				}
				let url = app.globalData.get_request_url("apply");
				uni.request({
					url: url,
					method: 'POST',
					data: post_data,
					success: (res) => {
						if (res.data.code == 200) {
							uni.showToast({
								title: res.data.msg,
								duration: 2000
							})
							setTimeout(() => {
								uni.navigateBack()
								uni.showToast({
									title: '等待审核哦',
									icon: 'none',
									duration: 2000
								})
							}, 2000)
						} else {
							uni.showToast({
								title: res.data.msg,
								icon: 'none',
								duration: 3000
							})
							setTimeout(() => {
								uni.navigateBack()
							}, 3000)
						}
					}
				})
			},
			
			// 拨打电话
			telFun(e) {
				var phone = e.currentTarget.dataset.tel || null;
				const res = uni.getSystemInfoSync();
				// ios系统默认有个模态框
				if (res.platform == 'ios') {
					uni.makePhoneCall({
						phoneNumber: phone,
						success() {
							console.log('拨打成功了');
						},
						fail() {
							console.log('拨打失败了');
						}
					})
				} else {
					uni.showActionSheet({
						itemList: [phone, '呼叫'],
						success: function(res) {
							if (res.tapIndex == 1) {
								uni.makePhoneCall({
									phoneNumber: phone,
								})
							}
						}
					})
				}
			},
			OnEntryTime() {
				var radarlog_data = {
					pageUrl: "pages/apply/apply",
					name: "申请加盟商页面",
					remark: "申请加盟商页面停留时间",
					entryTime: this.entryTime,
				}
				uni.request({
					url: app.globalData.get_request_url("radarlog/add"),
					data: radarlog_data,
					method: "POST",
					success: (res) => {
						// console.log(res, "申请加盟商页面停留时间");
					}
				})
			},
		}
	}
</script>

<style lang="less">
	// page {
	// 	background-color: #f2f2f2;
	// }
	.apply_content{
		width: 100%;
		height: 100vh;
		background-image: url(https://t1.chowtaiking.com.cn/img/ruoyi/2025/03/30/80c6cb36892749d59eaeefbb6550a3f2.jpg);
	}

	.apply_list {
		padding: 20rpx;
		// background-color: white;
		color: white;

		.apply_item {
			display: flex;
			align-items: center;
			padding-bottom: 20rpx;
			// border-bottom: 1px solid #f2f2f2;
			// padding-top: 20rpx;

			.apply_item_name {
				width: 28%;
				font-size: 13px;
				font-weight: bold;
			}

			.picker {
				width: 70%;
				color: gray;
			}

			input {
				width: 70%;
				border-bottom: 1px solid #f2f2f2;
				font-size: 13px;
				padding: 15rpx;
			}
		}
	}

	.apply_btn {
		width: 100%;
		margin-top: 80rpx;
		// position: fixed;
		// bottom: 20rpx;

		button {
			background-color: red;
			color: white;
			width: 80%;
			height: 70rpx;
			text-align: center;
			line-height: 70rpx;
			border-radius: 40rpx;
		}
	}

	.footer {
		position: fixed;
		bottom: 0;
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;
		width: 100%;
		background-color: #fff;
		padding: 20rpx;
		text-align: center;
		box-sizing: border-box;

		.footer_item {
			display: flex;
			align-items: center;
			font-size: 13px;
			padding-bottom: 10rpx;
			.footer_item_phone{
				display: flex;
				align-items: center;
				image{
					width: 40rpx;
				}
			}
		}
	}
</style>